<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller\Home;

class ReMenController extends Controller
{
    //热门添加
    public function getAdd(Request $request)
    {   
      return view('admin.remen.add');
    }  

    //热门添加数据处理
    public function postInsert(Request $request)
    {   
    	
    	// dd($data);
    	$res = DB::table('remen')->insert($data);
    	// dd($remen);
    	if($res){
    		return view('admin.remen.index');
    	}else{
    		alert('添加失败');
    	}
    } 
    public function getIndex(Request $request)
    {   

    	//查询时指定每页显示的条数
        $num = $request->input('num');
        $data = $request->all();
        // dd($data);
        //判断是否搜索内容
        if($request->search){
            $remen = DB::table('remen')->where('title','like','%'.$request->search.'%')->paginate($num);
        }else{
            $remen = DB::table('remen')->paginate(5);
        }
    		return view('admin.remen.index',['remen'=>$remen,'request'=>$data]);

    }	

    //热门删除
    public function postDelete(Request $request){
    	$id = $request->only('id');
    	$res = DB::table('remen')->where('id',$id)->delete();
    	if ($res) {
    		# code...
    		echo 1;
    	}else{
    		echo 2;
    	}

    }
    public function getUpdate($id)
    {
    	// dd($id);

    	$remen = DB::table('remen')->where('id',$id)->first();
    	return view('admin.remen.edit',['remen'=>$remen]);
    }
    public function postSave(Request $request)
    {
    	// dd($request->all());
    	$data = $request->only('content','title');
    	$id = $request->only('id');
    	$res = DB::table('remen')->where('id',$id)->update($data);
    	// dd($res);
    	if($res == 1){
    		// alert('修改成功');
    		return redirect('admin/remen/index')->with('success','修改成功');
    	}else{
            return back()->with('error','修改失败');
    	}
    }
}
